package com.gotokeep.keep.demo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gotokeep.keep.video.FFmpegUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView imgPathView;
    private Button btnChooseImg;
    private Button btnMergeImg;
    private Button btnCutVideo;
    private Button btnCompress;
    private Button btnRotate;
    private Button btnReplaceAudio;

    private String srcPath;
    private String imgPath = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FFmpegUtils.initialize(this);

        imgPathView = findViewById(R.id.img_path);
        btnChooseImg = findViewById(R.id.btn_choose_image);
        btnMergeImg = findViewById(R.id.btn_merge_image);
        btnCutVideo = findViewById(R.id.btn_cut_video);
        btnCompress = findViewById(R.id.btn_compress);
        btnRotate = findViewById(R.id.btn_rotate_video);
        btnReplaceAudio = findViewById(R.id.btn_replace_audio);

        btnChooseImg.setOnClickListener(this);
        btnMergeImg.setOnClickListener(this);
        btnCutVideo.setOnClickListener(this);
        btnCompress.setOnClickListener(this);
        btnRotate.setOnClickListener(this);
        btnReplaceAudio.setOnClickListener(this);
        srcPath = "/sdcard/src.mp4";

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    2);
        }
    }

    @Override
    public void onClick(View view) {
        String result = "";
        switch (view.getId()) {
            case R.id.btn_choose_image:
                Intent chooseImgIntent = new Intent(Intent.ACTION_GET_CONTENT);
                chooseImgIntent.setType("image/*");
                startActivityForResult(Intent.createChooser(chooseImgIntent, "选择图片"), 1);
                break;
            case R.id.btn_merge_image:
                if (imgPath == null) {
                    break;
                }
                result = FFmpegUtils.mergeImage(srcPath, imgPath, 10, 10, "/sdcard/merge.mp4");
                break;
            case R.id.btn_cut_video:
                result = FFmpegUtils.cutVideo(srcPath, "/sdcard/cut.mp4", 0, 3);
                break;
            case R.id.btn_compress:
                result = FFmpegUtils.processVideo(srcPath, "/sdcard/compressed.mp4", 1080, 1920, 0, 6000);
                break;
            case R.id.btn_rotate_video:
                result = FFmpegUtils.rotateVideo(srcPath, 180, "/sdcard/rotate.mp4");
                break;
            case R.id.btn_replace_audio:
                result = FFmpegUtils.mixAudio("/sdcard/DCIM/KeepStory/Demo/170023_1.mp4",
                        "/storage/emulated/0/Android/data/com.gotokeep.keep/cache/story-bgm/bf2000f4bfb91940beba4d93bda75da5",
                        "/sdcard/DCIM/KeepStory/Demo/0.mp4");
                result = FFmpegUtils.mergeImage("/sdcard/DCIM/KeepStory/Demo/0.mp4",
                        "/sdcard/DCIM/KeepStory/Demo/KeeP1206315615.png",
                        0, 0,
                        "/sdcard/DCIM/KeepStory/Demo/out.mp4");
                break;
        }
        new AlertDialog.Builder(this)
                .setMessage("操作完毕！path = " + result)
                .show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Uri imgUri = data.getData();
            Cursor cursor = getContentResolver().query(imgUri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
            if (cursor != null) {
                cursor.moveToNext();
                imgPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                imgPathView.setText(imgPath);
                cursor.close();
            }
        }
    }
}
